#include "xPass.h"
#include "XRenderSystem.h"
#include "xTextureManager.h"
#include "xTexture.h"

namespace XE
{



	xPass::xPass(const  xString& name,xTechnique* pTechnique)
		:m_name(name.c_str()),m_IsClearColor(false),m_IsClearDepthStencil(false),
		m_ClearColor(xColor(0.0f,0.0f,0.0f,0.0f)),m_pTechnique(pTechnique),m_IsUpDateAnno(false)
	{


		//xSurface* m_newColorSurface;
		//xSurface* m_newDepthSurface;
	

	}

	xPass::~xPass()
	{
	
	}

	HRESULT xPass::Begin(XRenderSystem* pRender)
	{
		if(NeedupDateAnnotation())
		{
			upDateAnnotationparemeter();
		}

		//return X_OK;
          if(m_newColorSurface.isNull()==false)
		  {
			  m_oldColorSurface = pRender->GetRenderColorSuface(0);

			  if(FAILED(pRender->SetRenderColorSurface(0,m_newColorSurface.getPointer())))
			  {
				  return X_FAIL;
			  }
			  pRender->Clear(true,true,m_ClearColor);

		  }

		  if(m_newDepthSurface.isNull()==false)
		  {
			  m_oldDepthSurface = pRender->GetDepthStencilSurface();
			  if(FAILED( pRender->SetDepthStencilSurface(m_newDepthSurface.getPointer())))
				  return X_FAIL;
		  }
		  if(m_IsClearColor)
		  {
			    if(FAILED(pRender->Clear(true,m_IsClearDepthStencil,m_ClearColor)))
					return X_FAIL;

		  }

		  return X_OK;
	}

	HRESULT xPass::End(XRenderSystem* pRender)
	{
		//	return X_OK;
		if(m_oldColorSurface.isNull()==false)
		{
			if(FAILED(pRender->SetRenderColorSurface(0,m_oldColorSurface.getPointer())))
			{
				return X_FAIL;
			}
		}

		if(m_oldDepthSurface.isNull()==false)
		{

			if(FAILED( pRender->SetDepthStencilSurface(m_oldDepthSurface.getPointer())))
				return X_FAIL;
		}

		return X_OK;
	}


	void  xPass::SetRenderColorTexture(xTexturePtr	 pTexture)
	{
		m_pColorTexture = pTexture;
		if(m_pColorTexture.isNull())
		{
			m_newColorSurface.setNull();

		}else
		{
			m_newColorSurface = 	m_pColorTexture->GetSurface();
		}
        return;	

	}

	void  xPass::SetRenderDepthTexture(xTexturePtr pTexture)
	{
		m_DepthStencilTexture = pTexture;
		if(m_DepthStencilTexture.isNull())
		{
			m_newDepthSurface.setNull();
		}else
		{
           m_newDepthSurface = m_DepthStencilTexture->GetSurface();
		}
	 return;

	}


	void xPass::AddAnnotations(const xString& name,const xString& Value)
	{
		m_AnnotationMap.insert(std::make_pair(name,Value));
		m_IsUpDateAnno = true;
		return ;
	}




	void xPass::upDateAnnotationparemeter()
	{
		ParaMap::iterator it = m_AnnotationMap.find("RenderTexture");
		{
		
			if(it!=m_AnnotationMap.end())
			{
				xString RenderTargetName = it->second;
				xVector2 RenderSize(256,256);

				ParaMap::iterator sizeit = m_AnnotationMap.find("Size");
				if(sizeit!=m_AnnotationMap.end())
				{
					RenderSize = xHelp::StringToVector2(sizeit->second);
				}
				xTexturePtr RenderTexture =xTextureManager::getSingleton().GetByName(RenderTargetName);
				if(RenderTexture.isNull())
				{
					RenderTexture = xTextureManager::getSingleton().CreateRenderTexture(RenderTargetName, RenderSize.x,RenderSize.y);

				}
				SetRenderColorTexture(RenderTexture);

			
			}
		}


	



		m_IsUpDateAnno = false;
	}
	

}